Load the packages

library(Seurat)
library(data.table)
library(NMF)
library(rsvd)
library(Rtsne)
library(ggplot2)
library(cowplot)
library(sva)
library(igraph)
library(cccd)
library(KernSmooth)
library(beeswarm)
library(stringr)
library(formatR)
source("../tools.R")
library(DESeq2)

The function will be used in the follow

Analysis based on cell size

According to the previous analysis on sample group,remove the group hc001 and cell size 2um ### Read data ### Data QA

human.only.pro <- Load_data(data_dir = "../data/human.txt")
important.genes <- c("ABCB5", "KRT19", "ACTB", "KRT12", "KRT5", "GAPDH", "KRT3", 
    "PAX6", "WNT7A", "KRT14", "TP63", "KRT10")
human.only.pro <- human.only.pro[, colnames(human.only.pro)[unlist(lapply(colnames(human.only.pro), 
    function(x) return(str_split(x, "_")[[1]][2]))) %in% c("10um", "20um", "6um")]]
human.only.pro <- human.only.pro[, colnames(human.only.pro)[!unlist(lapply(colnames(human.only.pro), 
    function(x) return(str_split(x, "_")[[1]][1]))) %in% "hc001"]]

Split the data according to whether the gene ITGB4 is Negative or negative

ITGB4 <- as.integer(human.only.pro["ITGB4", ])
Positive.idx <- which(ITGB4 > 0)
Negative.idx <- which(ITGB4 == 0)
Positive.data <- human.only.pro[, Positive.idx, drop = FALSE]
Negative.data <- human.only.pro[, Negative.idx, drop = FALSE]

Create Seurat object and not caculate DESeq

Positive.pbmc <- DESeq_SeuratObj(X = Positive.data, min.cells = 10, min.genes = 2)
## [1] "Scaling data matrix"
## 
  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |=================================================================| 100%

Positive.sample.group <- unlist(lapply(Positive.pbmc@cell.names, function(x) return(str_split(x, 
    "_")[[1]][1])))
Positive.sample.cellsize <- unlist(lapply(Positive.pbmc@cell.names, function(x) return(str_split(x, 
    "_")[[1]][2])))

Positive.pbmc <- SetIdent(Positive.pbmc, cells.use = Positive.pbmc@cell.names, 
    ident.use = Positive.sample.cellsize)
Negative.pbmc <- DESeq_SeuratObj(X = Negative.data, min.cells = 10, min.genes = 2)
## [1] "Scaling data matrix"
## 
  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |=================================================================| 100%

Negative.sample.group <- unlist(lapply(Negative.pbmc@cell.names, function(x) return(str_split(x, 
    "_")[[1]][1])))
Negative.sample.cellsize <- unlist(lapply(Negative.pbmc@cell.names, function(x) return(str_split(x, 
    "_")[[1]][2])))

Negative.pbmc <- SetIdent(Negative.pbmc, cells.use = Negative.pbmc@cell.names, 
    ident.use = Negative.sample.cellsize)

Accordind to the Dispersion vs Avearge expression of Positive and Negative data on ITGB4,they tell us that the although they have similar shape and trend,dispersion of Positive data is more significant than Negative in some genes.

Step 1: analysis on Positive data

Figure Explore

First,use the plot,eg. Barplot,Violin…,we can explore some message from sample

Group_Bar(Positive.pbmc@raw.data, group = Positive.sample.group)

Group_Bar(Positive.pbmc@raw.data, group = Positive.sample.cellsize)

VlnPlot(Positive.pbmc, features.plot = important.genes[important.genes %in% 
    rownames(Positive.pbmc@raw.data)], y.lab.rot = 90)  # Violinn plot of gene ITGB in all sample

Dimensionality reduction

PCA and tSNE

Here,do the dimensionality reduction using the PCA, tSNE method 

It will take a long time to caculate significant pcs.So,here we use the default value

Positive.pbmc <- PCA.TSNE(object = Positive.pbmc, pcs.compute = FALSE, num.pcs = 28)

After the PCA and tSNE,try plot: Featureplot of ITGB4,four var.genes,PCA plot,tSNE plot…

FeaturePlot(object = Positive.pbmc, features.plot = important.genes[important.genes %in% 
    rownames(Positive.pbmc@raw.data)], pt.size = 1, no.legend = FALSE, reduction.use = "pca")  # ITGB4 gene in part dataset

FeaturePlot(object = Positive.pbmc, features.plot = important.genes[important.genes %in% 
    rownames(Positive.pbmc@raw.data)], pt.size = 1, no.legend = FALSE, reduction.use = "tsne")  # ITGB4 gene in part dataset

DimPlot(Positive.pbmc, reduction.use = "tsne", pt.size = 4)  #  grour by sample

DimPlot(Positive.pbmc, reduction.use = "pca", pt.size = 4)  #  grour by sample

DimHeatmap(Positive.pbmc, reduction.type = "pca", check.plot = FALSE)

The Faetureplot of ABCB5, KRT19, ACTB, KRT12, KRT5, GAPDH, KRT3, PAX6, WNT7A, KRT14, TP63, KRT10based on PCA shows that,they only has high expression level in few samples,and expresss lowly in most sample.It means that may be these important genes express differently across sample.The plot also tell us the gene KRT5,GAPDH,PAXX6,KRT14 have more higher expression level than the other important genes.It is consistent with the result of violin plot. About the heatmap,we only show the gene ITGB4 And the FeatureHeatmap and Heamap also comfirm this phenomeno.We try the other four variable genes,which has the similar result as gene ITGB4 But the tSNE and * PCA * plot show that, the sample can not be split apparently.The result may be is not good based on the PCA and tSNE method.

Differential expression

Next,we will have analysis on gene differential expression.Find maker genes across sample.We use the method: **wilcox test**
# Finds markers (differentially expressed genes) for each of the identity
# classes in a dataset
Positive.markers <- FindAllMarkers(Positive.pbmc, test.use = "bimod", print.bar = FALSE)
head(Positive.markers)
##                       p_val  avg_logFC pct.1 pct.2     p_val_adj cluster
## RP11-217O12.1 3.162288e-106  2.2181485 1.000 1.000 5.366087e-102     6um
## AC009501.4     3.875662e-72  1.9076241 0.770 0.375  6.576610e-68     6um
## FLNA           8.284662e-34  1.1208948 0.656 0.308  1.405824e-29     6um
## MT-TL1         7.019260e-30 -0.9094591 0.279 0.893  1.191098e-25     6um
## LMOD3          7.878372e-28  0.3401618 0.246 0.130  1.336881e-23     6um
## CYP24A1        1.671717e-27  1.0038167 0.852 0.221  2.836737e-23     6um
##                        gene
## RP11-217O12.1 RP11-217O12.1
## AC009501.4       AC009501.4
## FLNA                   FLNA
## MT-TL1               MT-TL1
## LMOD3                 LMOD3
## CYP24A1             CYP24A1

We check whether the important genes are still in the marker genes we found from the DESeq analysis. the genes:ACTB, GAPDH, KRT3, KRT10 are still in the marker genes.

Bar plot of gene’s p.val

Positive.heatmap <- Heatmap_fun(genes = important.genes[important.genes %in% 
    rownames(Positive.pbmc@raw.data)], tpm.data = Positive.pbmc@scale.data, 
    condition = unique(as.character(Positive.pbmc@ident)), all.condition = as.character(Positive.pbmc@ident))
## There ara 3 conditions
## Whether creat data accurate 0
NMF::aheatmap(Positive.heatmap[[2]], Rowv = NA, Colv = NA, annCol = Positive.heatmap[[1]], 
    scale = "none")

We have find all marker genes across sample,there are 1165 significant genes(adjust p-value <0.05) in all marker genes.

Next,Spectral k-means clustering on single cells based on PCA

Positive.pbmc <- KClustDimension(Positive.pbmc, reduction.use = "pca", k.use = 3)
clusters.pca <- Positive.pbmc@meta.data$kdimension.ident
DimPlot(Positive.pbmc, pt.size = 4, group.by = "kdimension.ident")

Spectral k-means clustering on single cells based on tSNE

Positive.pbmc <- KClustDimension(Positive.pbmc, reduction.use = "tsne", k.use = 3)
clusters.tsne <- Positive.pbmc@meta.data$kdimension.ident
DimPlot(Positive.pbmc, pt.size = 4, group.by = "kdimension.ident", reduction.use = "tsne")

Step 2: analysis on Negative data

Figure Explore

First,use the plot,eg. Barplot,Violin…,we can explore some message from sample

Group_Bar(Negative.pbmc@raw.data, group = Negative.sample.group)

Group_Bar(Negative.pbmc@raw.data, group = Negative.sample.cellsize)

VlnPlot(Negative.pbmc, features.plot = important.genes[important.genes %in% 
    rownames(Negative.pbmc@raw.data)], y.lab.rot = 90)  # Violinn plot of gene ITGB in all sample

Dimensionality reduction

PCA and tSNE

Here,do the dimensionality reduction using the PCA, tSNE method 

It will take a long time to caculate significant pcs.So,here we use the default value

Negative.pbmc <- PCA.TSNE(object = Negative.pbmc, pcs.compute = FALSE, num.pcs = 28)

After the PCA and tSNE,try plot: Featureplot of ITGB4,four var.genes,PCA plot,tSNE plot…

FeaturePlot(object = Negative.pbmc, features.plot = important.genes[important.genes %in% 
    rownames(Negative.pbmc@raw.data)], pt.size = 1, no.legend = FALSE, reduction.use = "pca")  # ITGB4 gene in part dataset

FeaturePlot(object = Negative.pbmc, features.plot = important.genes[important.genes %in% 
    rownames(Negative.pbmc@raw.data)], pt.size = 1, no.legend = FALSE, reduction.use = "tsne")  # ITGB4 gene in part dataset

DimPlot(Negative.pbmc, reduction.use = "tsne", pt.size = 4)  #  grour by sample

DimPlot(Negative.pbmc, reduction.use = "pca", pt.size = 4)  #  grour by sample

DimHeatmap(Negative.pbmc, reduction.type = "pca", check.plot = FALSE)

The Faetureplot of ABCB5, KRT19, ACTB, KRT12, KRT5, GAPDH, KRT3, PAX6, WNT7A, KRT14, TP63, KRT10based on PCA shows that,they only has high expression level in few samples,and expresss lowly in most sample.It means that may be these important genes express differently across sample.The plot also tell us the gene KRT5,GAPDH,PAXX6,KRT14 have more higher expression level than the other important genes.It is consistent with the result of violin plot. About the heatmap,we only show the gene ITGB4 And the FeatureHeatmap and Heamap also comfirm this phenomeno.We try the other four variable genes,which has the similar result as gene ITGB4 But the tSNE and * PCA * plot show that, the sample can not be split apparently.The result may be is not good based on the PCA and tSNE method.

Differential expression

Next,we will have analysis on gene differential expression.Find maker genes across sample.We use the method: **wilcox test**
# Finds markers (differentially expressed genes) for each of the identity
# classes in a dataset
Negative.markers <- FindAllMarkers(Negative.pbmc, test.use = "bimod", print.bar = FALSE)
head(Negative.markers)
##                      p_val  avg_logFC pct.1 pct.2    p_val_adj cluster
## RP11-217O12.1 2.115782e-95  3.0279498 0.989 0.956 3.221278e-91     6um
## AC009501.4    2.354411e-62  3.3451197 0.695 0.155 3.584591e-58     6um
## PRRG3         7.469107e-48  1.2538526 0.158 0.080 1.137172e-43     6um
## ACTG1P12      1.838296e-41  0.7535906 0.053 0.142 2.798806e-37     6um
## MYL6P1        4.841405e-39  0.3823263 0.084 0.334 7.371040e-35     6um
## MT-CO2        1.383075e-38 -2.0157102 0.789 0.972 2.105732e-34     6um
##                        gene
## RP11-217O12.1 RP11-217O12.1
## AC009501.4       AC009501.4
## PRRG3                 PRRG3
## ACTG1P12           ACTG1P12
## MYL6P1               MYL6P1
## MT-CO2               MT-CO2

We check whether the important genes are still in the marker genes we found from the DESeq analysis. the genes:KRT19, ACTB, KRT5, GAPDH, KRT3, PAX6, KRT14, TP63 are still in the marker genes.

Bar plot of gene’s p.val

Negative.heatmap <- Heatmap_fun(genes = important.genes[important.genes %in% 
    rownames(Negative.pbmc@raw.data)], tpm.data = Negative.pbmc@scale.data, 
    condition = unique(as.character(Negative.pbmc@ident)), all.condition = as.character(Negative.pbmc@ident))
## There ara 3 conditions
## Whether creat data accurate 0
NMF::aheatmap(Negative.heatmap[[2]], Rowv = NA, Colv = NA, annCol = Negative.heatmap[[1]], 
    scale = "none")

We have find all marker genes across sample,there are 990 significant genes(adjust p-value <0.05) in all marker genes.

Next,Spectral k-means clustering on single cells based on PCA

Negative.pbmc <- KClustDimension(Negative.pbmc, reduction.use = "pca", k.use = 3)
clusters.pca <- Negative.pbmc@meta.data$kdimension.ident
DimPlot(Negative.pbmc, pt.size = 4, group.by = "kdimension.ident")

Spectral k-means clustering on single cells based on tSNE

Negative.pbmc <- KClustDimension(Negative.pbmc, reduction.use = "tsne", k.use = 3)
clusters.tsne <- Negative.pbmc@meta.data$kdimension.ident
DimPlot(Negative.pbmc, pt.size = 4, group.by = "kdimension.ident", reduction.use = "tsne")

Differential expression use DESeq2 packages

When use the DESeq,it must require the gene count matrix satisify that: every gene contains at least one zero, cannot compute log geometric means. So have to take another method to handle data,but I do not know whether it is reasonable.Just try!!!

Positive

condition.p <- unlist(lapply(Positive.pbmc@cell.names, function(x) return(str_split(x, 
    "_")[[1]][2])))
Positive.xdds <- DESeq_CT(count.data = Positive.pbmc@raw.data, condition.1 = condition.p)
plotDispEsts(Positive.xdds, main = "Per-gene Dispersion")

Do the DESeq test across all cells with sample group.And get all the significant genes between two groups(p.value < 0.05)

Positive.DESeqGenes <- DESeq_result(Positive.xdds, condition = condition.p)
Positive.DESeqGenes.v <- as.vector(Positive.DESeqGenes)
library(VennDiagram)
grid.draw(venn.diagram(Positive.DESeqGenes.v[1:3], filename = NULL, fill = c("dodgerblue", 
    "goldenrod1", "darkorange1")))

Negative

condition.n <- unlist(lapply(Negative.pbmc@cell.names, function(x) return(str_split(x, 
    "_")[[1]][2])))
Negative.xdds <- DESeq_CT(count.data = Negative.pbmc@raw.data, condition.1 = condition.n)
plotDispEsts(Negative.xdds, main = "Per-gene Dispersion")

Do the DESeq test across all cells with sample group.And get all the significant genes between two groups(p.value < 0.05)

Negative.DESeqGenes <- DESeq_result(Negative.xdds, condition = condition.n)
Negative.DESeqGenes.v <- as.vector(Negative.DESeqGenes)
library(VennDiagram)
grid.draw(venn.diagram(Negative.DESeqGenes.v[1:3], filename = NULL, fill = c("dodgerblue", 
    "goldenrod1", "darkorange1")))